package com.yx.definition;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.*;
import org.springframework.util.StringUtils;
import java.util.*;



/**
 * @author yinxing
 * @date 2022-09-13
 */
public class YinXingApplication {


    public static ConfigurableApplicationContext runSpringboot(String appName, Class source, String... args) {
        SpringApplicationBuilder builder = createSpringApplicationBuilder(appName, source, args);
        return builder.run(args);
    }

    public static SpringApplicationBuilder createSpringApplicationBuilder(String appName, Class source, String... args) {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(new Class[]{source});
        return builder;
    }

    private static String getProfile(ConfigurableEnvironment environment) {
        String[] activeProfiles = environment.getActiveProfiles();
        List<String> profiles = Arrays.asList(activeProfiles);
        List<String> activeProfileList = new ArrayList(profiles);
        String profile;
        if (activeProfileList.isEmpty()) {
            profile = "dev";
            activeProfileList.add(profile);
        } else {
            if (activeProfileList.size() != 1) {
                throw new RuntimeException("同时存在环境变量:[" + StringUtils.arrayToCommaDelimitedString(activeProfiles) + "]");
            }

            profile = (String)activeProfileList.get(0);
        }

        return profile;
    }

}
